package com.tencent.miniproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.miniproject.R;
import com.tencent.miniproject.base.BaseRecyclerViewAdapter;
import com.tencent.miniproject.been.NeighborRecommand;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.listener.OnClickItemVoteListener;
import com.tencent.miniproject.ui.DetailActivity;
import com.tencent.miniproject.ui.MapActivity;
import com.tencent.miniproject.ui.OthersUserBillActivity;
import com.tencent.miniproject.utils.DateUtil;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;


public class NeighborRecommandAdapter extends BaseRecyclerViewAdapter<NeighborRecommand> {
    private Context mContext;
    private OnClickItemVoteListener mVoteListener;
    private Double mLongtitude;
    private Double mLatitude;

    public NeighborRecommandAdapter(Context context,Double longitude,Double latitude) {
        mContext = context;
        mLongtitude = longitude;
        mLatitude = latitude;
    }

    public void setOnClickVoteListener(OnClickItemVoteListener voteListener){
        mVoteListener = voteListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_content, parent, false);
        return new NeighborRecommandHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((NeighborRecommandHolder)holder).bindView(mDataList.get(position));
    }

    class NeighborRecommandHolder extends BaseRvHolder{
        @BindView(R.id.iv_img)
        ImageView mIvImg;
        @BindView(R.id.civ_user_img)
        CircleImageView mCivUserImg;
        @BindView(R.id.tv_user_name)
        TextView mTvUserName;
        @BindView(R.id.tv_store)
        TextView mTvStore;
        @BindView(R.id.tv_cost)
        TextView mTvCost;
        @BindView(R.id.tv_location)
        TextView mTvLocation;
        @BindView(R.id.tv_distance)
        TextView mTvDistance;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_votes)
        TextView mTvVotes;
        @BindView(R.id.tv_comments)
        TextView mTvComments;
        @BindView(R.id.ll_main_item)
        LinearLayout mLlMainItem;

        NeighborRecommandHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void bindView(final NeighborRecommand neighborRecommand) {
            Glide.with(mContext)
                    .load(neighborRecommand.getImgUrl())
                    .placeholder(R.drawable.photo_empty_photo)
                    .into(mIvImg);
            Glide.with(mContext)
                    .load(neighborRecommand.getHead_url())
                    .placeholder(R.drawable.photo_empty_photo)
                    .into(mCivUserImg);
            mTvUserName.setText(neighborRecommand.getUsername());
            mTvStore.setText(neighborRecommand.getStore());
            mTvCost.setText("￥"+neighborRecommand.getCost());
            mTvLocation.setText(neighborRecommand.getPosition());
            if (neighborRecommand.getDistance() == null){
                mTvDistance.setVisibility(View.GONE);
            }
            mTvDistance.setText(neighborRecommand.getDistance()+"米");
            mTvTime.setText(DateUtil.dateFormatDay(neighborRecommand.getReleaseTime()));
            mTvVotes.setText(neighborRecommand.getVotes()+"");
            mTvComments.setText(neighborRecommand.getComments()+"");
            mLlMainItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, DetailActivity.class);
                    intent.putExtra("id",neighborRecommand.getId());
                    intent.putExtra("longitude",mLongtitude);
                    intent.putExtra("latitude",mLatitude);
                    mContext.startActivity(intent);
                }
            });
            mTvStore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpToMapActivity(neighborRecommand);
                }
            });
            mCivUserImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, OthersUserBillActivity.class)
                            .putExtra("userId", neighborRecommand.getUserId()));
                }
            });
            mTvLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jumpToMapActivity(neighborRecommand);
                }
            });
        }
    }

    /**
     * 跳转地图
     * @param neighborRecommand neighborRecommand
     */
    public void jumpToMapActivity(NeighborRecommand neighborRecommand){
        mContext.startActivity(new Intent(mContext, MapActivity.class).putExtra("type", Constant.MAP_POST_ID)
                .putExtra("postId", neighborRecommand.getId())
                .putExtra("lng", mLongtitude)
                .putExtra("lat", mLatitude));
    }
}
